package com.javabi.unix.parser.uptime;

import java.math.BigDecimal;

import com.javabi.unix.line.LineNumberProcessor;
import com.javabi.unix.line.LineTokenizer;

public class UptimeParser extends LineNumberProcessor<Uptime> {

	private Uptime uptime = null;

	@Override
	public Uptime getResult() {
		if (uptime == null) {
			throw new IllegalStateException("uptime not set");
		}
		return uptime;
	}

	@Override
	public boolean processLine(String line, int number) {
		if (line.isEmpty()) {
			return true;
		}

		LineTokenizer tokenizer = new LineTokenizer(line);
		tokenizer.next("load average:");
		String oneMinute = tokenizer.next(",");
		String fiveMinute = tokenizer.next(",");
		String fifteenMinute = tokenizer.remaining();

		Uptime uptime = new Uptime();
		uptime.setOneMinute(new BigDecimal(oneMinute));
		uptime.setFiveMinute(new BigDecimal(fiveMinute));
		uptime.setFifteenMinute(new BigDecimal(fifteenMinute));
		this.uptime = uptime;

		return true;
	}
}
